﻿Public Class FLGuru

    'member variables
    Private _cGuru As CGuru
    Private _dView As DataView

    ''' <summary>
    ''' digunakan untuk menentukan data guru yang ditampilkan adalah data yang difilter atau tidak
    ''' </summary>
    ''' <remarks></remarks>
    Private _isFiltered As Boolean
    Private _excludeId As List(Of Integer)
    Private _filterString As String

    'events
    Public Event DataTerpilih()

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Me.Text = My.Settings.NamaAplikasi & " [Pilih Guru]"
        _isFiltered = False
    End Sub

    ''' <summary>
    ''' FLGuru baru dengan menampilkan data guru kecuali guru yang telah ditentukan melalui ExcludeIdGuru
    ''' </summary>
    ''' <param name="ExcludeIdGuru"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal ExcludeIdGuru As List(Of Integer))
        Me.New()
        If ExcludeIdGuru.Count > 0 Then
            _isFiltered = True
            _excludeId = ExcludeIdGuru
            'set fitler string 
            For Each item As Integer In _excludeId
                _filterString &= item & ","
            Next
            'hapus koma terakhir pada _filterString
            _filterString = _filterString.Trim(",")
        End If
        

    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown, LabelJudul.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    'Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
    '    'menampilkan nomor urut pada datagridview
    '    DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1

    'End Sub

    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub



    Private Sub btnMinimize_Click(sender As System.Object, e As System.EventArgs)
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


    '-----------------------------------------------------------------------------------------------
#End Region

    Public Property SelectedIDGuru As Integer
    Public Property SelectedNamaGuru As String
    Public Property SelectedNIPGuru As String

    Private Sub DGMaster_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGMaster.CellContentDoubleClick
        BtnPilih.PerformClick()
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1

    End Sub

    Private Sub BtnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBatal.Click
        Me.Close()
    End Sub

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        BtnPilih.PerformClick()
    End Sub

    Private Sub BatalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BatalToolStripMenuItem.Click
        Me.BtnBatal.PerformClick()
    End Sub

    Private Sub FLGuru_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _cGuru = New CGuru
        'tampilkan data ke datagrid 
        TampilkanData()
        'jika data lebih dari 0 enable button
        If DGMaster.RowCount > 0 Then
            BtnPilih.Enabled = True
        End If
    End Sub

    Private Sub TampilkanData()
        _dView = _cGuru.getAll.DefaultView
        DGMaster.DataSource = _dView

        'set filter
        If _isFiltered Then
            _dView.RowFilter = "id not in (" & _filterString & ")"
        End If
    End Sub

    Private Sub DGMaster_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGMaster.SelectionChanged
        'masukkan data terpilih ke dalam property
        If DGMaster.RowCount > 0 Then
            Me.SelectedIDGuru = DGMaster.CurrentRow.Cells("CID").Value
            Me.SelectedNamaGuru = DGMaster.CurrentRow.Cells("CNama").Value
            Me.SelectedNIPGuru = DGMaster.CurrentRow.Cells("CNIP").Value
        End If

    End Sub

    Private Sub BtnPilih_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPilih.Click
        RaiseEvent DataTerpilih()
        Me.Close()
    End Sub

  
    Private Sub TBNama_TextChanged(sender As System.Object, e As System.EventArgs) Handles TBNama.TextChanged
        _dView.RowFilter = cbFilter.Text & " like '" & TBNama.Text.Trim & "%'"
    End Sub

    Private Sub cbFilter_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbFilter.SelectedIndexChanged
        TBNama.Clear()
    End Sub
End Class